package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Role;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.qo.QueryObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller
@RequestMapping("/role")
public class RoleController {


    private IRoleService roleService;
    private IPermissionService permissionService;

    @Autowired
    public void setRoleService(IRoleService roleService) {
        this.roleService = roleService;
    }
    @Autowired
    public void setPermissionService(IPermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @RequiredPermission(name="角色页面",expression="role:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject queryObject){
        model.addAttribute("PageInfo",roleService.query(queryObject));
        return "/role/list";
    }
    @RequiredPermission(name="角色删除",expression="role:delete")
    @RequestMapping("/delete")
    public String delete(Long id ){
        roleService.delete(id);
        return "redirect:/role/list";
    }

    //去到新增页面
    @RequiredPermission(name="角色新增/编辑",expression="role:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id ,Model model ){
        model.addAttribute("permission",permissionService.selectAll());
        if(id != null){
            List<Role> permissions = roleService.selectPermission(id);
            model.addAttribute("selfPermission",permissions);
            model.addAttribute("role",roleService.selectByPrimaryKey(id));
        }
        return  "/role/input";
    }
    @RequiredPermission(name="角色新增/编辑",expression="role:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids){
        if(role.getId() != null){
            roleService.update(role,ids);
        }else {
            roleService.insert(role,ids);
        }
        return "redirect:/role/list";
    }

}
